package movies.repositoryHibernate;

import java.util.List;
import movies.domain.Movie;
import movies.repository.IMovieRepository;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 *
 * @author Hanna_Aliakseichykava
 */
public class MovieRepository extends HibernateDaoSupport
implements IMovieRepository{

    @Override
    public Movie saveMovie(Movie movie) {
        
        Integer id = (Integer)getHibernateTemplate().save(movie);
        movie.setId(id);
        return movie;
    }

    @Override
    public Movie getMovieById(int id) {
        List < Movie > movies = getHibernateTemplate().
             findByNamedQuery("GetMovieById", id);

        if (movies.size() == 1) {
            return movies.get(0);
        } else {
            return null;
        }
    }

    @Override
    public void deleteMovie(Movie movie) {
        getHibernateTemplate().delete(movie);
    }

    @Override
    public List<Movie> getMoviesByName(String name) {
        List < Movie > movies = getHibernateTemplate().
             findByNamedQuery("GetMoviesByName", name);

        if (movies.size() > 0) {
            return movies;
        } else {
            return null;
        }
    }

    @Override
    public List<Movie> getMoviesByYear(int year) {
        List < Movie > movies = getHibernateTemplate().
             findByNamedQuery("GetMoviesByYear", year);

        if (movies.size() > 0) {
            return movies;
        } else {
            return null;
        }
    }

    @Override
    public List<Movie> getAllMovies() {
        List < Movie > movies = getHibernateTemplate().
             findByNamedQuery("GetAllMovies");

        if (movies.size() > 0) {
            return movies;
        } else {
            return null;
        }
    }



}
